$(function (){
    let app = new Vue({
        el: "#app",
        data: {
            sysTitle: "固定资源管理系统",
            id: "10011001101",
            password: "12345678",
            userType: "0",
            Info: ["用户账号：","用户密码：","用户类型："],
            backImageSrc: "../../images/common/back.jpg",
            aMessage: [{name: "忘记密码", href: "newPwd.html"},{name: "管理员注册", href: "newUser.html"}]
        },
        created(){

        },
        methods: {
            handleLogin(){
                if(this.id === "")
                {
                    alert("请输入账号!");
                    return;
                }
                if(this.password === "")
                {
                    alert("请输入密码!");
                    return;
                }
                if(this.userType === "-1")
                {
                    alert("请选择用户类型!");
                    return;
                }
                this.loginIn();
            },
            loginIn(){
                if(this.userType === "0")
                {
                    axios.get("http://localhost:16/mangers/"+this.id).then((res)=>{
                        var result = res.data;
                        if(result.flag)
                        {
                            if(result.data.password === this.password)
                            {
                                alert("登录成功！欢迎"+result.data.name+"使用固定资源管理系统^_^");
                                sessionStorage.setItem("userID", this.id);
                                sessionStorage.setItem("userName", result.data.name);
                                sessionStorage.setItem("userType", this.userType);
                                sessionStorage.setItem("userClass", "管理员");
                                sessionStorage.setItem("userDepartment", "管理部");
                                sessionStorage.setItem("userImageSrc", "images/upload/" + result.data.image);
                                location.href = "../../index.html";
                            }
                            else {
                                alert("密码不正确，请重新输入！");
                                this.password = "";
                            }
                        }else {
                            alert("系统中不存在此管理员^…^ | 用户类型不正确！！！")
                        }
                    });
                }else {
                    axios.get("http://localhost:16/employees/"+this.id).then((res)=>{
                        var result = res.data;
                        if(result.flag)
                        {
                            if(result.data.password === this.password)
                            {
                                /*alert("登录成功！欢迎"+result.data.name+"使用固定资源管理系统^_^");*/
                                sessionStorage.setItem("userID", this.id);
                                sessionStorage.setItem("userName", result.data.name);
                                sessionStorage.setItem("userType", this.userType);
                                sessionStorage.setItem("userClass", "用户");
                                sessionStorage.setItem("userImageSrc", "images/upload/" + result.data.image);
                                axios.get("http://localhost:16/departments/"+result.data.did).then((res)=>{
                                    if(res.data.flag){
                                       /* sessionStorage.setItem("userDepartment", res.data.data.name);*/
                                        if(result.data.manger)
                                        {
                                            sessionStorage.setItem("userDepartment", res.data.data.name + "主管");
                                        }
                                        else {
                                            sessionStorage.setItem("userDepartment",  res.data.data.name + "员工");
                                        }
                                        location.href = "../../index.html";
                                    }else {
                                        alert("登录失败，请稍后再试！");
                                    }
                                });
                            }
                            else {
                                alert("密码不正确，请重新输入！");
                                this.password = "";
                            }
                        }else {
                            alert("系统中不存在此管理员^…^ | 用户类型不正确！！！")
                        }
                    });
                }

            }

        }
    });
});